package com.lanou3g.demo.servlet;

import com.lanou3g.demo.base.BaseServlet;
import com.lanou3g.demo.dao.OrderDao;
import com.lanou3g.demo.entity.BookEntity;
import com.lanou3g.demo.entity.OrderEntity;
import com.lanou3g.demo.entity.OrderItemEntity;
import com.mchange.v2.lang.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "MainSelectServlet",urlPatterns = "/main/*")
public class MainSelectServlet extends BaseServlet {

    // 显示订单
    public void orderList(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException, SQLException {
        // 获取session
        HttpSession session = request.getSession();
        // 获取用户id
        String uid = (String) session.getAttribute("uid");
        // 查询出该用户所对应的订单
        OrderEntity order = OrderDao.selectOrder(uid);

        if(order == null){
            request.getRequestDispatcher("/main/body").forward(request,response);
        }else {
            // 获取订单id
            String oid = order.getOid();

            // 查询出该订单号多对应的订单项集合
            List<OrderItemEntity> orderItem = OrderDao.selectOrderItem(oid);


            for (OrderItemEntity orderItemEntity : orderItem) {
                BookEntity book = OrderDao.selectBook(orderItemEntity.getBid());
                orderItemEntity.setBookEntity(book);
            }

            request.setAttribute("orderItem",orderItem);
            request.setAttribute("order",order);

            request.getRequestDispatcher("/WEB-INF/jsps/order/list.jsp").forward(request,response);
        }
    }

    // 显示书籍
    public void bookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/left.jsp").forward(request,response);
    }

    // 显示购物车
    public void cartList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/cart/list.jsp").forward(request,response);
    }

    // 显示用户登入页面
    public void userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/user/login.jsp").forward(request,response);
    }

    // 显示用户注页面
    public void userRegist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/user/regist.jsp").forward(request,response);
    }

    // 显示top页面
    public void  top(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/top.jsp").forward(request,response);
    }

    // 显示left页面
    public void left(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/left.jsp").forward(request,response);
    }

    // 显示body页面
    public void body(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/jsps/body.jsp").forward(request,response);
    }
}
